//-----------------------------------------------------------------------
// <copyright file="ProvinceDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class ProvinceDAOBase
    {
        #region Common methods
        public virtual Province CreateProvinceFromReader(IDataReader reader)
        {
            Province item = new Province();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ProvinceId"))) item.ProvinceId = (long)reader["ProvinceId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateProvinceFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateProvince methods
            
        public virtual void CreateProvince(Province province)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProvinceInsert");
                
                database.AddInParameter(dbCommand, "@Title", DbType.String, province.Title);
                database.AddOutParameter(dbCommand, "@ProvinceId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                province.ProvinceId = (long)database.GetParameterValue(dbCommand, "@ProvinceId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateProvinceException, ex);
            }
        }

        #endregion

        #region UpdateProvince methods
        
        public virtual void UpdateProvince(Province province)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProvinceUpdate");            
                
                database.AddInParameter(dbCommand, "@ProvinceId", DbType.Int64, province.ProvinceId);
                database.AddInParameter(dbCommand, "@Title", DbType.String, province.Title);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateProvinceException, ex);
            }
        }
        
        #endregion

        #region DeleteProvince methods
        public virtual void DeleteProvince(long provinceId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProvinceDelete");
                
                database.AddInParameter(dbCommand, "@ProvinceId", DbType.Int64, provinceId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteProvinceException, ex);
            }
        }

        #endregion

        #region GetProvince methods
        
        public virtual Province GetProvince(long provinceId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProvinceGet");
                
                database.AddInParameter(dbCommand, "@ProvinceId", DbType.Int64, provinceId);
                
                Province province =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        province = CreateProvinceFromReader(reader);
                        reader.Close();
                    }
                }
                return province;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetProvinceException, ex);
            }
        }
        
        #endregion

        #region GetProvinceList methods
        public virtual ProvinceCollection GetProvinceList(ProvinceColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProvinceGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ProvinceCollection provinceCollection = new ProvinceCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Province province = CreateProvinceFromReader(reader);
                        provinceCollection.Add(province);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return provinceCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetProvinceListException, ex);
            }
        }
        
        public virtual ProvinceCollection GetProvinceList(ProvinceColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetProvinceList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetProvinceList methods
        public virtual ProvinceCollection GetProvinceListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProvinceGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ProvinceCollection provinceCollection = new ProvinceCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Province province = CreateProvinceFromReader(reader);
                        provinceCollection.Add(province);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return provinceCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetProvinceListException, ex);
            }
        }
        
        public virtual ProvinceCollection GetProvinceListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetProvinceListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}